package lib.wzh.com.libcore.framework.validated.constraintValidator;

import cn.hutool.core.util.ArrayUtil;
import lib.wzh.com.libcore.framework.validated.annotation.SpecifyList;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author lib
 * @Date 2021/12/11 21:00
 * @ClassName SpecifyListConstraintValidator
 * @Version v1.0
 */

public class SpecifyListConstraintValidator implements ConstraintValidator<SpecifyList,Integer> {

    private final Set<Integer> set = new HashSet<>();

    @Override
    public void initialize(SpecifyList constraintAnnotation) {
        int[] value = constraintAnnotation.value();
        if(ArrayUtil.isNotEmpty(value)){
            for (int v : value) {
                set.add(v);
            }
        }
    }

    @Override
    public boolean isValid(Integer i, ConstraintValidatorContext constraintValidatorContext) {
        return set.contains(i);
    }
}
